上一篇有跟大家分享一下 Docker 跟 Linux 之間的關係,本篇教大家如何安裝與操作~
更新 apt-get$sudo apt-get update
安裝軟件包以允許apt通過HTTPS使用存儲庫$sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ gnupg-agent \ software-properties-common
添加Docker的官方GPG金鑰$curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
設置穩定的存儲庫sudo add-apt-repository \ "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) \ stable"
安裝最新版本的Docker Engine和容器$sudo apt-get install docker-ce docker-ce-cli containerd.io -y
安裝完成後可以查看 docker 版本,也可以得知是否有安裝成功$docker -v
$ sudo groupadd docker $ sudo usermod -aG docker $USER
$newgrp docker $sudo systemctl restart docker.service
最後使用指令,來查看是否正常$docker run hello-world
如果有出現
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
就代表說成功了
既然已經安裝完成的話,我們就來分享如何使用 Docker
mkdir docker-test
cd docker-test
# app.py
print("Docker Master!")
# 使用官方的 Python 基礎映像
FROM python:3.8-slim
# 將當前目錄內容複製到映像檔中的 /app 目錄
COPY . /app
# 設定工作目錄為 /app
WORKDIR /app
# 設定啟動命令,運行 Python 應用程式
CMD ["python", "app.py"]
docker build -t docker-test .
docker run docker-test
原則上可以看到終端回傳
Docker Master!
docker images
docker ps -a
回傳畫面如下
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8abb877cc078 docker-test "python app.py" 16 seconds ago Exited (0) 14 seconds ago gifted_mclean
f75796b8a83b hello-world "/hello" 37 minutes ago Exited (0) 37 minutes ago festive_yonath
6211e451682b hello-world "/hello" About an hour ago Exited (0) About an hour ago distracted_driscoll
以上就大功告成拉
本篇希望大家能更加瞭解 Docker 的使用方式以及如何建立 Dockerfile
https://philipzheng.gitbook.io/docker_practice/image/create